<?php
namespace App\Dao\config;
use App\Dao\BaseDao;
use App\Models\config\ConfigModel;
use function Couchbase\defaultDecoder;
use Illuminate\Support\Facades\DB;
use App\Services\system\SystemLogServices;
use App\Exceptions\ApiException as HOException;
use Hlcreation\Basic\Helper;
class UploadDao extends BaseDao{

    public function setModel(){
        return ConfigModel::class;
    }


    public function Fileinsert($data){


        DB::beginTransaction();
        try{
            $where = [
                'creator_uid'=>$data['creator_uid'],
                'type'=>$data['type'],
            ];
            $get = $this->models::where($where)->get();
            if ($get) $this->models::where($where)->forceDelete();
            $res['creator_uid'] = $data['creator_uid']; $res['created_at'] = $data['created_at']; $res['config_tab_id'] = $data['config_tab_id'];
            unset($data['creator_uid']); unset($data['created_at']); unset($data['config_tab_id']);

            foreach ($data as $k=>&$v){
                $this->models::insert([
                    'menu_name'=>$k,
                    'value'=>$v,
                    'creator_uid'=>$res['creator_uid'],
                    'created_at'=>$res['created_at'],
                    'config_tab_id'=>$res['config_tab_id'],
                    'desc'=>$data['desc'],
                    'type'=>$data['type'],
                    'status'=>$data['status']
                ]);
            }



            // dd($arr);
            /*$url = env('APP_CONTRACT_API_URL', 'http://192.168.1.199:84')."/adminapi/luming/login/saasStorage";
            $arr_json= json_encode($arr);
            $arr_encode=base64_encode($arr_json);
            //dd($arr);
            $res=Helper::httpPost($url,$arr_encode);*/
            /* }*/
            /*if ($res['code'] != 200) throw new HOException('电子合同配置异常');*/


            //插入日志
            $LogServices = app()->make(SystemLogServices::class);
            $LogServices->SaveLog($res['creator_uid'],'创建系统配置','配置项名称:'.'阿里云上传配置'.'--配置项ID:'.$res['config_tab_id']);

            DB::commit();
            return true;
        }catch (\Exception $exception){
            DB::rollBack();
            throw new HOException($exception->getMessage());
        }

    }



    public function fileInfos($uid,$type){
        $field = [
            'id',
            'menu_name',
            'value',
            'desc',
            'status',
            'creator_uid',
            'config_tab_id',
            'type',
            'created_at'
        ];
        return  ConfigModel::where('type',$type)->where('creator_uid',$uid)->select($field)->get()->toArray();
    }




    /**
     * 配置信息
     */
    public function  UploadInfo($uid){
        //查询配置分类表


        $place = $this->models::select(['menu_name','value','status','config_tab_id','type'])

            ->where(['creator_uid'=>$uid])
            ->orderBy('id','desc')->get();


        $res = $this->formatData($place);
        return $res;

    }



    /**
     * 获取状态
     */
    public function  GetStatus($uid,$config_tab_id){

        return $this->formatData($this->models::where(['creator_uid'=>$uid])->whereIn('config_tab_id',$config_tab_id)->select(['status'])->first());


    }


}
